let arr1 = [-1,-2,-3,-4]
let arr2 = [1,3,-1,5,6,-2,6,7,-3]
// 法1：暴力破解
function getMax(arr) {
    let max = -Infinity
    for(let i = 0; i < arr.length; i++) {
        let product = arr[i]
        for(let j = i+1; j < arr.length; j++) {
            product *= arr[j]
            max = Math.max(max, product)
        }
    }
    return max
}
console.log(getMax(arr1), getMax(arr2));

// 法2：dp动规划，dp[n] = Math.max(dp[n-1]*nums[n], nums[n])
function getMax2(arr) {
let min = max = arr[0]
let dp = [arr[0]]
for(let i = 1; i < arr.length; i++) {
    if(arr[i] < 0) {
        [min, max] = [max, min]
    }
    max = Math.max(max*arr[i], arr[i])
    min = Math.min(min*arr[i], arr[i])
    dp[i] = max
}
return Math.max(...dp)
}
console.log(getMax2(arr1), getMax2(arr2))